package org.ldx.lotterysystem.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.ldx.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import org.ldx.lotterysystem.common.exception.ControllerException;
import org.ldx.lotterysystem.common.pojo.CommonResult;
import org.ldx.lotterysystem.common.utils.JacksonUtil;
import org.ldx.lotterysystem.controller.param.CreatePrizeParam;
import org.ldx.lotterysystem.controller.param.PageParam;
import org.ldx.lotterysystem.controller.result.FindPrizeListResult;
import org.ldx.lotterysystem.service.PictureService;
import org.ldx.lotterysystem.service.PrizeService;
import org.ldx.lotterysystem.service.dto.PrizeDTO;
import org.ldx.lotterysystem.service.dto.PageListDTO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

@Slf4j
@RestController
public class PrizeController {
    @Resource
    private PictureService pictureService;
    @Resource
    PrizeService prizeService;
    @RequestMapping("/pic/upload")
    public String uploadPic(MultipartFile multipartFile){
        return pictureService.savePicture(multipartFile);
    }

    /**
     * 创建奖品
     * @param param 表单数据
     * @param file 上传的图片
     * @return
     */
    @RequestMapping("/prize/create")
    //RequestPart 用于前端传递的数据是表单数据 multipart/from-data
    // 后面绑定传递数据的名字是什么
    public CommonResult<Long> createPrize(@Validated @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile file ){
        log.info("createPrize param:{}",param);
        log.info("createPrize param:{}",
                JacksonUtil.writeValueAsString(param));
        return CommonResult.success(prizeService.createPrize(param,file));

    }
    @RequestMapping("/prize/find-list")//没用 @RequestBody 因为param要从url中进行读取
    public CommonResult<FindPrizeListResult> findPrizeList( PageParam param){
        log.info("findPrizeList param:{}",
                JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> prizeList = prizeService.findPrizeList(param);
        return CommonResult.success(convertToFindPrizeListResult(prizeList));
    }

    private FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> prizeList) {
        if(null == prizeList){
            throw new ControllerException(ControllerErrorCodeConstants.FIND_LIST_ERROR);
        }
        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(prizeList.getTotal());
        result.setRecords(
                prizeList.getRecords().stream().map(
                        prizeDTO -> {
                            FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                            prizeInfo.setPrizeName(prizeDTO.getName());
                            prizeInfo.setDescription(prizeDTO.getDescription());
                            prizeInfo.setPrice(prizeDTO.getPrice());
                            prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                            return prizeInfo;
                        }
                ).collect(Collectors.toList())
        );
        return result;
    }
}
